package com.example.service.usr.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.context.PageResult;
import com.example.entity.ProductCateDO;
import com.example.entity.ProductDO;
import com.example.exception.exception;
import com.example.mapper.ProductMapper;
import com.example.service.usr.ProductService;
import com.example.utils.FileUtil;
import com.example.vo.product.UsrProductDetail;
import com.example.vo.product.UsrProductPageReq;
import com.example.vo.product.UsrProductPageResp;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private FileUtil fileUtil;

    @Override
    public UsrProductDetail findById(Long productId) {
        ProductDO product = productMapper.selectById(productId);
        if (product == null) {
            throw new exception("商品不存在");
        }
        return BeanUtil.toBean(product, UsrProductDetail.class);
    }

    @Override
    public PageResult<UsrProductPageResp> productPage(UsrProductPageReq req) {
        Page<UsrProductPageResp> page = new Page<>(req.getPage(), req.getSize());
        MPJLambdaWrapper<ProductDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(ProductDO.class);
        wrapper.selectAs(ProductCateDO::getCateName, "type");
        wrapper.leftJoin(ProductCateDO.class, ProductCateDO::getId, ProductDO::getCateId);
        if (StringUtils.isNotBlank(req.getKeyword())) {
            wrapper.like(ProductDO::getName, req.getKeyword());
        }
        if (req.getCateId() != null) {
            wrapper.eq(ProductDO::getCateId, req.getCateId());
        }
        wrapper.orderByDesc(ProductDO::getCreateTime);
        IPage<UsrProductPageResp> iPage = productMapper.selectJoinPage(page, UsrProductPageResp.class, wrapper);
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public ResponseEntity<UrlResource> findPicByName(String name) {
        String format = StringUtils.substringAfterLast(name, ".").toLowerCase(Locale.ROOT);
        if (!"png".equals(format) && !"jpg".equals(format) && !"jpeg".equals(format) && !"gif".equals(format)) {
            throw new exception("图片格式不支持");
        }
        MediaType mediaType = MediaType.parseMediaType("image/" + format);
        UrlResource resource = fileUtil.getFile(name);
        return ResponseEntity.ok()
                .contentType(mediaType)
                .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }
}
